/*
  SimpleMQTTClient.ino
  The purpose of this exemple is to illustrate a simple handling of MQTT and Wifi connection.
  Once it connects successfully to a Wifi network and a MQTT broker, it subscribe to a topic and send a message to it.
  It will also send a message delayed 5 seconds later.
*/

#include "EspMQTTClient.h"
#include <Adafruit_NeoPixel.h>
EspMQTTClient client(
  "HUAWEI-354",
  "88888888",
  "120.77.16.8",  // MQTT Broker server ip
  "deng",   // Can be omitted if not needed
  "jialei",   // Can be omitted if not needed
  "TestClient",     // Client name that uniquely identify your device
  61613              // The MQTT port, default to 1883. this line can be omitted
);
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
 
// 控制 WS2812 灯条的引脚编号
#define PIN        D3
 
//定义控制的 LED 数量
#define NUMPIXELS 1
 
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
//相邻 LED 之间的延迟，单位毫秒
#define DELAYVAL 500
void setup()
{
  Serial.begin(115200);
  #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
  #endif
  // END of Trinket-specific code.
 
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)

  // Optionnal functionnalities of EspMQTTClient : 
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
  client.enableLastWillMessage("TestClient/lastwill", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  // Subscribe to "mytopic/test" and display received message to Serial
  client.subscribe("deng/led", [](const String & payload) {
    Serial.println(payload);
      pixels.clear();  
      int receivedChar =payload.toInt();
      Serial.println(receivedChar);
	  
      /*
	  { "LampSwitch":"1",
		"LED_R":0,
		"LED_G":151,
		"LED_B":254}
	  */

      //const int receivedChar =(const int & payload) ;
   /* if(payload=="1"){
      Serial.println("66666");
      for(int i=0; i<NUMPIXELS; i++) 
        pixels.setPixelColor(i, pixels.Color(0, 0, 150));
    
      }
      if(payload=="2"){
      Serial.println("66666");
      for(int i=0; i<NUMPIXELS; i++) 
        pixels.setPixelColor(i, pixels.Color(0, 150, 150));
    
      }*/
      for(int i=0; i<NUMPIXELS; i++) 
        pixels.setPixelColor(i, pixels.Color(0, 0, receivedChar));
    
      
       pixels.show();
  });

  // Publish a message to "mytopic/test"
  client.publish("deng/led", "This is a message"); // You can activate the retain flag by setting the third parameter to true

  // Execute delayed instructions
  client.executeDelayed(5 * 1000, []() {
    client.publish("deng/led", "This is a message sent 5 seconds later");
  });
}

void loop()
{
  client.loop();
}
